package com.usian.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.article.mapper.ApAuthorMapper;
import com.usian.article.service.ApAuthorService;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.Date;
import java.util.List;

@Service
public class ApAuthorServiceImpl extends ServiceImpl<ApAuthorMapper, ApAuthor> implements ApAuthorService {


    @Override
    public ResponseResult saveApAuthor(ApAuthor apAuthor) {

        //参数校验
        if (apAuthor == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //添加创建时间
        apAuthor.setCreatedTime(new Date());

        //执行添加操作
        boolean save = save(apAuthor);

        //判断添加操作是否成功
        if (save){
            //操作成功返回
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else {
            //操作失败返回
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }

    }

    @Override
    public ApAuthor findByUserId(Integer userId) {

        //参数校验
        if (userId == null){
            return  null;
        }

        //跟就条件进行查询
        List<ApAuthor> list = list(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getUserId, userId));

        //判断数字中是否有值
        if (list.size()>0){
            //放回数组中第一个值
            return list.get(0);
        }

        return null;
    }

    @Override
    public ApAuthor findByName(String name) {

        if (StringUtils.isBlank(name)){
            return null;
        }

        ApAuthor apAuthor = this.getOne(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getName, name));

        return apAuthor;
    }

    @Override
    public ApAuthor findByWmUserId(Integer userId) {
        //参数校验
        if (userId == null){
            return  null;
        }

        //跟就条件进行查询
        List<ApAuthor> list = list(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getWmUserId, userId));

        //判断数字中是否有值
        if (list.size()>0){
            //放回数组中第一个值
            return list.get(0);
        }

        return null;
    }
}
